Option

sealed class Option<out T>

Representation of an optional value. The instance might be either Some value or None.

Parameters

T

Type of the optional value.

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
inline fun all(predicate: (T) -> Boolean): Boolean

Returns the result of applying the predicate to the value if this is Some or true if this is None.

Link copied to clipboard
inline fun any(predicate: (T) -> Boolean): Boolean

Returns the result of applying the predicate to the value if this is Some or false if this is None.

Link copied to clipboard
abstract fun asIterable(): Iterable<T>

Returns an iterable that wraps this Option returning its value if it is defined, or an empty iterable if the option is empty.

Link copied to clipboard
abstract fun asSequence(): Sequence<T>

Returns a sequence that wraps this Option returning its value if it is defined, or an empty sequence if the option is empty.

Link copied to clipboard
inline fun filter(predicate: (T) -> Boolean): Option<T>

Returns the same Some if the predicate is satisfied for the value. Otherwise returns a None.

Link copied to clipboard
inline fun <R> filterIsInstance(): Option<R>

Returns the same Some casted to type R if it is R. Otherwise returns a None.

Link copied to clipboard
inline fun filterNot(predicate: (T) -> Boolean): Option<T>

Returns the same Some if the predicate is not satisfied for the value. Otherwise returns a None.

Link copied to clipboard
inline fun <R> flatMap(transform: (T) -> Option<R>): Option<R>

Maps value of a Some to a new Option using transform or returns the same None.

Link copied to clipboard
inline fun <R> fold(default: R, transform: (T) -> R): R
inline fun <R> fold(default: () -> R, transform: (T) -> R): R

Returns result of applying transform on the value of Some or default if this is None.

Link copied to clipboard
inline fun forEach(action: (T) -> Unit)

Runs action if this is a Some. Returns Unit without any action if this is None.

Link copied to clipboard
abstract fun get(): T

Gets the value of a Some or throws an exception.

Link copied to clipboard
abstract fun getOrNull(): T?

Gets the value of a Some or null if this is a None.

Link copied to clipboard
abstract fun iterator(): Iterator<T>

Returns a singleton iterator returning the option's value if it is defined, or an empty iterator if the option is empty.

Link copied to clipboard
inline fun <R> map(transform: (T) -> R): Option<R>

Maps value of a Some using transform or returns the same None.

Link copied to clipboard
inline fun none(predicate: (T) -> Boolean): Boolean

Returns false if the predicate is met by the value if this is Some or true otherwise.

Link copied to clipboard
inline fun <R> toLeft(right: () -> R): Either<T, R>

Returns a Right containing the given argument right if this is empty, or a Left containing this option's value if it is defined.

Link copied to clipboard
abstract fun toList(): List<T>

Returns a singleton list containing the option's value if it is defined, or an empty list if the option is empty.

Link copied to clipboard
inline fun <L> toRight(left: () -> L): Either<L, T>

Returns a Left containing the given argument left if this is empty, or a Right containing this option's value if it is defined.

Link copied to clipboard
infix fun <R> zip(other: Option<R>): Option<Pair<T, R>>

Returns Some containing a Pair of values of this and other if both Options are Some. Otherwise returns None.

inline fun <T1, R> zip(other: Option<T1>, transform: (T, T1) -> R): Option<R>

Returns Some containing the result of applying transform to both values of this and other if both Options are Some. Otherwise returns None.

Properties

Link copied to clipboard
val isDefined: Boolean

Returns true if the option is Some. Otherwise returns false.

Link copied to clipboard
abstract val isEmpty: Boolean

Returns true if the option is None. Otherwise returns false.

Link copied to clipboard
val isNotEmpty: Boolean

Returns true if the option is Some. Otherwise returns false.

Inheritors

Link copied to clipboard
Link copied to clipboard

Extensions

Link copied to clipboard
operator fun <T> Option<T>.contains(element: T): Boolean

Tests whether the Option contains the given element.

Link copied to clipboard
fun <T> Option<Try<T>>.evert(): Try<Option<T>>

Moves inner Try outside of the outer Option.

Link copied to clipboard
fun <T> Option<Try<T>>.flatten(): Option<T>

Returns Some if this Some contains a Success. Otherwise returns None.

fun <T> Option<Iterable<T>>.flatten(): List<T>

Returns nested List if this is Some. Otherwise returns an empty List.

fun <T> Option<Option<T>>.flatten(): Option<T>

Transforms a nested Option to a not nested Option.

Link copied to clipboard
inline fun <T> Option<T>.getOrElse(default: () -> T): T

Gets the value of a Some or default value if this is None.

Link copied to clipboard
inline fun <T> Option<T>.orElse(default: () -> Option<T>): Option<T>

Returns this Option if this is a Some or default if this is None.

Link copied to clipboard
fun <A, B> Option<Pair<A, B>>.unzip(): Pair<Option<A>, Option<B>>

Transforms an Option of a Pair into a Pair of an Option of the first value and an Option of the second value.

fun <A, B, C> Option<Triple<A, B, C>>.unzip(): Triple<Option<A>, Option<B>, Option<C>>

Transforms an Option of a Triple into a Triple of an Option of the first value, an Option of the second value, and an Option of the third value.